package com.le.tester.javaThink.fourth.concurrency;

import java.util.concurrent.TimeUnit;

/**
 * createTime：2021/9/13 14:25
 * description：后台线程创建的线程将会都是后台线程
 */

class Daemon implements Runnable {

    private Thread[] t = new Thread[10];

    @Override
    public void run() {
        for (int i = 0; i < t.length; i++) {
            t[i] = new Thread(new DaemonSpawn());
            t[i].start();
            System.out.print("DaemonSpawn " + i + " started,");
        }
        for (int i = 0; i < t.length; i++)
            System.out.print("t[" + i + "].isDaemon() = " + t[i].isDaemon() + ", ");
        while (true)
            //线程即将让出cpu
            Thread.yield();

    }
}

class DaemonSpawn implements Runnable {

    @Override
    public void run() {
        while (true)
            Thread.yield();
    }
}

public class Daemons {
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new Daemon());
        thread.setDaemon(true);
        thread.start();
        System.out.print("thread.isDaemon() = " + thread.isDaemon() + ", ");
        TimeUnit.MILLISECONDS.sleep(1000);
    }
}
